﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BarCode.AccessDao;
using BarCode.Bean;
using BarCode.Common;
using BarCode.Module;
using MySql.Data.MySqlClient;

namespace BarCode.Forms
{
    public partial class frmSelect : Form
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(frmSelect));

        private DB01Dao db01;

        private MySqlHelper db;

        public frmSelect()
        {
            InitializeComponent();
            db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void frmSelect_Load(object sender, EventArgs e)
        {
            //TmpOutRoomMdbDao tmpOutRoomMdbDao = new TmpOutRoomMdbDao();
            //DataTable dtUnPack = tmpOutRoomMdbDao.selectUnPackList();
            //if (dtUnPack == null)
            //{
            //    gridPack.Visible = false;
            //}
            //else
            //{
            //    gridPack.Visible = true;
            //    gridPack.DataSource = dtUnPack;
            //}            
            //tmpOutRoomMdbDao.Release();
            refreshGirdData();
        }

        /// <summary>
        /// 添加序号
        /// </summary>
        private void gridPack_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in gridPack.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 删除
        /// </summary>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            Console.WriteLine("删除");
            if (gridPack.SelectedRows.Count != 1)
            {
                MessageUtil.Warn("请选择待封燃气表批号！");
            }
            else
            {
                Console.WriteLine("选中");
                DataGridViewRow selectedRow = gridPack.SelectedRows[0];
                string orderID;
                string metersIDstart;
                string metersIDend;
                if (selectedRow.Cells[2].Value.ToString().Contains("G"))
                {
                    Console.WriteLine("1带有G");
                    orderID = selectedRow.Cells[1].Value.ToString();
                    metersIDstart = selectedRow.Cells[3].Value.ToString();
                    metersIDend = selectedRow.Cells[4].Value.ToString();
                }
                else
                {
                    orderID = selectedRow.Cells[2].Value.ToString();
                    metersIDstart = selectedRow.Cells[5].Value.ToString();
                    metersIDend = selectedRow.Cells[6].Value.ToString();
                }
                
                Console.WriteLine("order"+orderID);
                Console.WriteLine(metersIDstart);
                Console.WriteLine(metersIDend);
                int i = db01.delete(orderID, metersIDstart, metersIDend);
                Console.WriteLine(i);
                refreshGirdData1();
                return ;
            }
        }

        private void frmSelect_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }


        /// <summary>
        ///  跳转封箱
        /// </summary>
        private void s_Click(object sender, EventArgs e)
        {
            if (gridPack.SelectedRows.Count != 1)
            {
                MessageUtil.Warn("请选择待封燃气表批号！");
            }
            else
            {
                ShowPackForm();
            }
        }

        /// <summary>
        ///  跳转封箱
        /// </summary>
        private void ShowPackForm()
        {
            LogUtil.Info("封箱页面准备启动...");
            DataGridViewRow selectedRow = gridPack.SelectedRows[0];
            Pack_BarCode packForm = new Pack_BarCode();
            packForm.drPack = selectedRow;
            this.Hide();
            packForm.ShowDialog();
            this.Show();
            refreshGirdData();
        }

        private void gridPack_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ShowPackForm();
        }


        private void refreshGirdData()
        {
            TmpOutRoomMdbDao tmpOutRoomMdbDao = new TmpOutRoomMdbDao();
            DataTable dtUnPack = tmpOutRoomMdbDao.selectUnPackList();
            if (dtUnPack == null)
            {
                gridPack.Visible = false;
            }
            else
            {
                gridPack.Visible = true;
                gridPack.DataSource = dtUnPack;
            }
            tmpOutRoomMdbDao.Release();
        }


        private void refreshGirdData1()
        {
            TmpOutRoomMdbDao tmpOutRoomMdbDao = new TmpOutRoomMdbDao();
            DataTable dtUnPack = tmpOutRoomMdbDao.selectUnPackList1();
            if (dtUnPack == null)
            {
                gridPack.Visible = false;
            }
            else
            {
                gridPack.Visible = true;
                gridPack.DataSource = dtUnPack;
            }
            tmpOutRoomMdbDao.Release();
        }
        private void gridPack_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
